#ifndef TFFMPEGTHREAD_H
#define TFFMPEGTHREAD_H

#include <QThread>
#include <QDateTime>
#include <QAudioOutput>
#include "tffmpegsupporter.h"
#include "tffmpegvideoplaythread.h"
#include "tffmpegaudioplaythread.h"
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libavdevice/avdevice.h>
#include <libavformat/version.h>
#include <libavutil/time.h>
#include <libavutil/mathematics.h>
#include <libavutil/ffversion.h>
#include <libavutil/imgutils.h>
#include <libswresample/swresample.h>
}

class TFFmpegThread : public QThread
{
    Q_OBJECT
public:
    explicit TFFmpegThread(QObject *parent = nullptr);
    ~TFFmpegThread() override;

    //打印支持的库
    static void showLib();

    //启动
    void playMedia(const QString &nurl);
    //停止
    void closeMedia();
    //获取/设置媒体地址
    QString getUrl();
    void setUrl(const QString &nurl);

    //初始化
    bool init();
    //初始化参数
    void initOption();
    //初始化输入
    bool initInput();
    //初始化视频
    bool initVideo();
    //初始化音频
    bool initAudio();
    //初始化其它
    void initOther();

    //释放对象
    void freeMember();
    //释放对象(帧)
    void freeMember(AVFrame *nframe);
    //释放对象(数据包)
    void freeMember(AVPacket *npacket);

public slots:

protected:
    void run() override;

private:
    volatile bool m_stopped = true;        //线程停止标志位
    QString m_url;                         //媒体地址
    QString m_transport = "tcp";           //传送协议

    AVDictionary *m_options = nullptr;     //参数对象
    AVPacket *m_packet = nullptr;          //待解码临时包

    quint64 m_duration = 0;                //视频或音频时长
    QString m_formatName;                  //文件格式
    qint64 m_startTime;                    //解码开始时间

    TFFmpegVideoPlayThread *m_ffmpegVideoPlayThread = nullptr;    //视频播放线程
    TFFmpegAudioPlayThread *m_ffmpegAudioPlayThread = nullptr;    //音频播放线程

    TFFCustomPlayer &m_ffcustomPlayer = TFFmpegSupporter::get()->getFFCustomPlayer();

signals:
    void sigGetVideoFrame(const QImage &nvideoframe);
    void sigMediaFinish();
    void sigLog(const QString &nlogstr);

};

#endif // TFFMPEGTHREAD_H
